﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.IO;
using System.Text.RegularExpressions;
/// <summary>
///TextFileLoader 的摘要说明
/// </summary>
  /**//**//**//// <summary>
       /// 文件加载
          /// </summary>
         /// <param name="FilePath">带文件名的路径</param>
        /// <param name="TableName">自定义的表名</param>
       /// <param name="FieldsInArray">自定义的表字段</param>
        /// <returns>DataSet</returns>
public static class TextFileLoader
{
    public static DataTable LoadTextFile(string FilePath, string TableName)
    {
        //DataSet ds = new DataSet();
        DataTable dt = new DataTable(TableName);

        FileStream fs = File.Open(FilePath, FileMode.Open, FileAccess.Read);
        StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default);
        string strRead;
        bool flag = true;
        strRead = sr.ReadLine();
        string[] FieldsInArray = FomatLine(strRead);
        //设置列头
        for (int i = 0; i < FieldsInArray.Length; i++)
        {
            dt.Columns.Add(new DataColumn(FieldsInArray[i], typeof(string)));
        }
        while (flag)
        {
            strRead = sr.ReadLine();

            if (!string.IsNullOrEmpty(strRead))
            {
                //整理去除换行符
                strRead = ReFormatString(strRead, sr);
                //分割行数据
                string[] aryVale = FomatLine(strRead);

                DataRow dr = dt.NewRow();
                for (int k = 0; k < aryVale.Length; k++)
                {
                    dr[FieldsInArray[k]] = aryVale[k];
                }
                dt.Rows.Add(dr);
            }
            else
            {
                flag = false;
            }
        }

        //ds.Tables.Add(dt);
        fs.Close();
        return dt;

    }

    public static DataTable LoadTextStream(Stream Filestream)
    {
        //DataSet ds = new DataSet();
        DataTable dt = new DataTable();

        Stream fs = Filestream ;
        StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default);
        string strRead;
        bool flag = true;
        strRead = sr.ReadLine();
        string[] FieldsInArray = FomatLine(strRead);
        //设置列头
        for (int i = 0; i < FieldsInArray.Length; i++)
        {
            dt.Columns.Add(new DataColumn(FieldsInArray[i], typeof(string)));
        }
        while (flag)
        {
            strRead = sr.ReadLine();

            if (!string.IsNullOrEmpty(strRead))
            {
                //整理去除换行符
                strRead = ReFormatString(strRead, sr);
                //分割行数据
                string[] aryVale = FomatLine(strRead);

                DataRow dr = dt.NewRow();
                for (int k = 0; k < aryVale.Length; k++)
                {
                    dr[FieldsInArray[k]] = aryVale[k];
                }
                dt.Rows.Add(dr);
            }
            else
            {
                flag = false;
            }
        }

        //ds.Tables.Add(dt);
        fs.Close();
        return dt;

    }

    public static string[] FomatLine(string line)
        {
        string[] aryVale = Regex.Split(line,"\"\t\"",RegexOptions.IgnoreCase);
        aryVale[0] = aryVale[0].Replace("\"", string.Empty);
        aryVale[aryVale.Length - 1] = aryVale[aryVale.Length - 1].Replace("\"",string.Empty).Replace("\r",string.Empty);
            return aryVale;
        }

    public static string ReFormatString(string strRead, StreamReader sr)
    {
        if (strRead.LastIndexOf('"') != strRead.Length - 1 || CountQuato(strRead)<102)
        {
             strRead = strRead + sr.ReadLine();
             return ReFormatString(strRead, sr);
        }
        else
        {
            return strRead;
        }
    }

    public static int CountQuato(string str)
    {
        int count = 0;
        foreach (char item in str.ToArray())
        {
            if (item == '\"')
            {
                count++;
            }
        }
        return count;
    }
}
